package cn.kent.simple;

/**
 * 27. 移除元素
 */
public class RemoveElement {
    public static void main(String[] args) {
        int[] nums = {0, 1, 2, 2, 3, 0, 4, 2};
        int val = 2;
        int i = removeElement2(nums, val);
        System.out.println(i);
        for (int num : nums) {
            System.out.println(num);
        }
    }

    /**
     *
     */
    public static int removeElement3(int[] nums, int val) {
        int i = 0;
        for (int num : nums) {
            if (num != val) {
                nums[i] = num;
                i++;
            }
        }
        return i;
    }

    /**
     * 前前 双指针
     */
    public static int removeElement2(int[] nums, int val) {
        int left = 0, right = 0;
        while (right < nums.length) {
            if (nums[right] != val) {
                nums[left] = nums[right];
                left++;
            }
            right++;
        }
        return left;
    }
}
